<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Class	: M_purchasertn
 * 
 * Table	: purchasertn
 *  
 * @author Eko Junaidi Salam
 *
 */
class M_purchasertn extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	/**
	 * Fungsi	: getAll
	 * 
	 * Untuk mengambil all-data
	 * 
	 * @param number $start
	 * @param number $page
	 * @param number $limit
	 * @return json
	 */
	function getAll($start, $page, $limit){
		$query  = $this->db->limit($limit, $start)->order_by('ID', 'DESC')->get('purchasertn')->result();
		$total  = $this->db->get('purchasertn')->num_rows();
		
		$data   = array();
		foreach($query as $result){
			$data[] = $result;
		}
		
		$json	= array(
						'success'   => TRUE,
						'message'   => "Loaded data",
						'total'     => $total,
						'data'      => $data
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: save
	 * 
	 * Untuk menambah data baru atau mengubah data lama
	 * 
	 * @param array $data
	 * @return json
	 */
	function save($data){
		$last   = NULL;
		
		$pkey = array('NOMOR_PURCHASE_RETURN'=>$data->NOMOR_PURCHASE_RETURN);
		
		if($this->db->get_where('purchasertn', $pkey)->num_rows() > 0){
			/*
			 * Data Exist
			 */			 
			$tmp = substr($data->SUB_TOTAL,3,strlen($data->SUB_TOTAL));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->SUB_TOTAL = $tmp;$tmp = substr($data->PPN,3,strlen($data->PPN));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->PPN = $tmp;$tmp = substr($data->TOTAL,3,strlen($data->TOTAL));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->TOTAL = $tmp;$tmp = substr($data->MATERAI,3,strlen($data->MATERAI));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->MATERAI = $tmp;	
			 
			$arrdatau = array('TANGGAL_PURCHASE_RETURN'=>(strlen(trim($data->TANGGAL_PURCHASE_RETURN)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_PURCHASE_RETURN)) : NULL),'NOMOR_LPB'=>$data->NOMOR_LPB,'APA_IMPORT'=>$data->APA_IMPORT,'KODE_SUPPLIER'=>$data->KODE_SUPPLIER,'NAMA_SUPPLIER'=>$data->NAMA_SUPPLIER,'ALAMAT_SUPPLIER'=>$data->ALAMAT_SUPPLIER,'TELPON_SUPPLIER'=>$data->TELPON_SUPPLIER,'FAX_SUPPLIER'=>$data->FAX_SUPPLIER,'CURRENCY'=>$data->CURRENCY,'KURS'=>$data->KURS,'SUB_TOTAL'=>$data->SUB_TOTAL,'PPN'=>$data->PPN,'TOTAL'=>$data->TOTAL,'MATERAI'=>$data->MATERAI,'DIBUAT_OLEH'=>$data->DIBUAT_OLEH,'JUMLAH_EDIT'=>$data->JUMLAH_EDIT,'TANGGAL_EDIT'=>(strlen(trim($data->TANGGAL_EDIT)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_EDIT)) : NULL),'INFO'=>$data->INFO);
			 
			$this->db->where($pkey)->update('purchasertn', $arrdatau);
			$last   = $data;
			
		}else{
			/*
			 * Data Not Exist
			 * 
			 * Process Insert
			 */
			 $tmp = substr($data->SUB_TOTAL,3,strlen($data->SUB_TOTAL));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->SUB_TOTAL = $tmp;$tmp = substr($data->PPN,3,strlen($data->PPN));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->PPN = $tmp;$tmp = substr($data->TOTAL,3,strlen($data->TOTAL));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->TOTAL = $tmp;$tmp = substr($data->MATERAI,3,strlen($data->MATERAI));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->MATERAI = $tmp;
			$arrdatac = array('NOMOR_PURCHASE_RETURN'=>$data->NOMOR_PURCHASE_RETURN,'TANGGAL_PURCHASE_RETURN'=>(strlen(trim($data->TANGGAL_PURCHASE_RETURN)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_PURCHASE_RETURN)) : NULL),'NOMOR_LPB'=>$data->NOMOR_LPB,'APA_IMPORT'=>$data->APA_IMPORT,'KODE_SUPPLIER'=>$data->KODE_SUPPLIER,'NAMA_SUPPLIER'=>$data->NAMA_SUPPLIER,'ALAMAT_SUPPLIER'=>$data->ALAMAT_SUPPLIER,'TELPON_SUPPLIER'=>$data->TELPON_SUPPLIER,'FAX_SUPPLIER'=>$data->FAX_SUPPLIER,'CURRENCY'=>$data->CURRENCY,'KURS'=>$data->KURS,'SUB_TOTAL'=>$data->SUB_TOTAL,'PPN'=>$data->PPN,'TOTAL'=>$data->TOTAL,'MATERAI'=>$data->MATERAI,'DIBUAT_OLEH'=>$data->DIBUAT_OLEH,'JUMLAH_EDIT'=>$data->JUMLAH_EDIT,'TANGGAL_EDIT'=>(strlen(trim($data->TANGGAL_EDIT)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_EDIT)) : NULL),'INFO'=>$data->INFO,'ID'=>$data->ID);
			 
			$this->db->insert('purchasertn', $arrdatac);
			$last   = $this->db->where($pkey)->get('purchasertn')->row();
			
		}
		
		$total  = $this->db->get('purchasertn')->num_rows();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil disimpan',
						'total'     => $total,
						"data"      => $last
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: delete
	 * 
	 * Untuk menghapus satu data
	 * 
	 * @param array $data
	 * @return json
	 */
	function delete($data){
		$pkey = array('NOMOR_PURCHASE_RETURN'=>$data->NOMOR_PURCHASE_RETURN);
		
		$this->db->where($pkey)->delete('purchasertn');
		
		$total  = $this->db->get('purchasertn')->num_rows();
		$last = $this->db->get('purchasertn')->result();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil dihapus',
						'total'     => $total,
						"data"      => $last
		);				
		return $json;
	}
}
?>